<template>
  <base-breadcrumb>
    <a-spin :spinning="tableLoading">
      <a-card>
        <titleName title="基本信息"></titleName>
        <a-form-model ref="ruleForm" :model="form" :rules="rules" :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
          :wrapperCol="{ lg: { span: 16 }, sm: { span: 16 } }">
          <a-row>
            <a-col :span="16">
              <a-form-model-item label="所属单位" prop="projectId">
                <org-tree-select v-model="form.projectId" isAutoSelectFirst checkStrictly disabled
                  @select="selectOrg"></org-tree-select>
                <!-- :disabled="pageType == 'looks'" -->
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="名称" prop="name">
                <a-input v-model="form.name" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="分包分供结算金额(含税)(元)：" prop="settlementAmount">
                <a-input-number :precision="2" v-model="form.settlementAmount" :disabled="pageType == 'looks'"
                  @change="handleNume" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="签证费用（元）：" prop="visaFee">
                <a-input-number :precision="2" v-model="form.visaFee" :disabled="pageType == 'looks'"
                  @change="handleNume" />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="签证费用占比（%）：" prop="visaFeeProportion">
                <a-input-number v-model="form.visaFeeProportion" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="填报人" prop="informant">
                <a-input v-model="form.informant" disabled />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item disabled label="填报时间：" prop="informantDate">
                <a-date-picker disabled v-model="form.informantDate"
                  @change="(date, dateString) => getTime(date, dateString, 'form', 'informantDate')" />
                <!-- <a-input  v-model="form.informantTime" :disabled="pageType == 'looks'" /> -->
              </a-form-model-item>
            </a-col>
            <a-col :md="24" :sm="24">
              <a-form-model-item label="结算范围" prop="settlementRange" :labelCol="{ lg: { span: 5 }, sm: { span: 5 } }"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 17 } }">
                <!-- <BaseTextarea :auto-size="{ minRows: 3, maxRows: 5 }" v-model="form.participants" /> -->
                <a-input v-model="form.settlementRange" type="textarea" :disabled="pageType == 'looks'" />
              </a-form-model-item>
            </a-col>
            <a-col :md="24" :sm="24">
              <a-form-model-item label="附件上传">
                <upload-list :businessCode="'BM19'" :disabled="pageType == 'looks'" :uploadArrys.sync="form.files"
                  @fileArrysData="handleFileUploaded" multiple></upload-list>
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </a-card>
      <div class="tool-btns page-btn-right-top" v-if="!isAudit && !isConsultation">

        <!-- <a-button v-if="pageType !== 'looks'" type="primary"  :loading="loading.SUBMITTED" @click="handleSave('DRAFT')"
          >保存草稿</a-button
        > -->
        <a-button type="primary" v-if="pageType !== 'looks'" :loading="loading.SUBMITTED"
          @click="handleSave('SUBMITTED')">提交</a-button>
        <a-button @click="handleClose">关闭</a-button>
      </div>
    </a-spin>
    <selectApprover :businessId="businessId" :processConfig="setProcessConfig"></selectApprover>
  </base-breadcrumb>
</template>
<script>

import { STable } from '@/components'
import UploadList from '@/components/UploadList'
import { getTwoSettlementOne, saveTwoSettlementList } from '@/api/businessAffairs/subcontractingettlement'
import moment from 'moment'
export default {
  name: 'a' + Date.now(),
  components: {
    STable,
    UploadList,
  },
  data() {
    return {
      tableLoading: false,
      loading: {
        DELETE: false,
        DRAFT: false,
        SUBMITTED: false,
        REJECTED: false
      },
      form: {
        projectId: '',
        name: '',
        settlementAmount: '',
        settlementRange: '',
        visaFee: '',
        visaFeeProportion: '',
        informant: '',
        informantDate: ''

      },
      rules: {
        settlementAmount: [{ required: true, message: '请输入' }],
        visaFee: [{ required: true, message: '请输入' }]
      },
      id: '',
      pageType: '',

      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null
    }
  },

  props: {
    // 是否作为审批详情
    isAudit: {
      type: Boolean,
      default: false
    },
    // 是否作为意见征询
    isConsultation: {
      type: Boolean,
      default: false
    },
  },
  async created() {
    this.pageType = this.$route.query.pageType
    if (this.$route.query.businessId) {
      this.pageType = 'looks'
    }
    this.getLists()
    this.processConfig = await this.$getConfigFn('comm_cost_plan')
  },
  methods: {
    getTime(date, dateString, type, name) {
      this[type][name] = dateString
      console.log(this[type][name])
    },
    // 获取详情
    getLists() {
      // console.log('wwwwwwwwwwwwwwwwwwwww')
      getTwoSettlementOne({ id: this.$route.query.id }).then((res) => {
        if (res.code == 200) {
          const data = res.data
          this.form = data
          this.form.files = res.data.reviewFileInfoVOList
          if (this.pageType !== 'looks') {
            this.form.informant = this.$userInfo().name
            this.form.informantDate = moment().format("YYYY-MM-DD")
          }
        }
      })
    },

    handleFileUploaded(list) {
      this.form.files = [...list]
    },

    //获取项目id
    selectOrg(val) {
      if (val && val.length) {
        this.form.branchComId = val[val.length - 2].orgId
      }
    },

    handleClose() {
      // this.$router.replace("/businessAffairs/businessOrganization/costPlanning")
      this.$multiTab.close(this.$route.fullpath)
    },
    handleSave(auditStatus) {
      this.form.auditStatus = auditStatus
      this.form.id = this.$route.query.id
      this.form.informantUserId = this.$userInfo().id
      if(this.form.files <= 0){
        this.$message.warning('请上传附件')
        return
      }
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading.SUBMITTED = true
          saveTwoSettlementList(this.form).then(res => {
            if (res.code === 200) {
              this.loading.SUBMITTED = false
              this.$message.success('成功')
              this.handleClose()
            } else {
              this.$message.warning('失败')
            }
          })
        }
      })
    },
    handleNume() {
      if (this.form.visaFee && this.form.settlementAmount) {
        this.form.visaFeeProportion = (this.form.visaFee / this.form.settlementAmount * 100).toFixed(2)
      }
    }
  }
}
</script>
<style lang="less" scoped>
@deep: ~'>>>';

.tool-btns {
  display: flex;
  justify-content: flex-end;

  @{deep} .ant-btn+.ant-btn {
    margin-left: 12px;
  }
}
</style>
  